import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import matplotlib.ticker as tk
from matplotlib.widgets import Cursor
plt.rcParams['font.sans-serif']=['SimHei']
#读取excel数据
data=pd.read_excel('学生课程成绩数据.xls')
fig=plt.figure(figsize=(12,6))#创建一个画布
rect=fig.patch
rect.set_facecolor('k')#画布的颜色
#添加标题
plt.suptitle('成绩分析图',color='white',
             fontsize=15,fontweight='bold',x=0.5,y=0.93,horizontalalignment='center',
             verticalalignment='center')
ax2=plt.axes([0,0.91,1,0.05])
rect=ax2.patch
rect.set_facecolor('#222A35')#设置绘图区域的背景
ax3=plt.axes([0.03,0.5,0.3,0.4])
ax3.set_facecolor('#222A35')
plt.xticks(())
plt.yticks(())
name=['单莹','陈卓','崔泽远','曹嘉明']
course=['生物化学','土壤学','体育（2）','专业劳动']
data_TB=[[] for i in range(4)]#二维空列表
data_1 = data[['姓名','课程名称','分数']]
data_array=np.array(data_1)#创建二维numpy数组，将data_1 数据拷贝过去
for i in range(len(data_array)):
    for j in range(4):
        for k in range(4):
            if(data_array[i][0]==name[j] and data_array[i][1]==course[k]):
                data_TB[j].append(data_array[i][2])
ax3_1=fig.add_axes([0.08,0.65,0.22,0.20])
ax3_1.set_facecolor('#222A35')
data_TB_T=np.transpose(data_TB)#转置
width=0.2
x=np.array([2,3.5,5,6.5])
ax3_1.bar(x-width/2*3,data_TB[0],width=0.2,label=course[0])
ax3_1.bar(x-width/2,data_TB[1],width=0.2,label=course[1])
ax3_1.bar(x+width/2,data_TB[2],width=0.2,label=course[2])
ax3_1.bar(x+width/2*3,data_TB[3],width=0.2,label=course[3])
plt.xlim([1,8.5])
ax3_1.yaxis.set_major_locator(tk.MultipleLocator(20))#设置主刻度
ax3_1.legend(loc=0,fontsize=4,markerscale=0.1)
ax3_1.set_xticks(x)
ax3_1.set_xticklabels(name)
ax3_1.tick_params('both',labelcolor='#BB9C48',labelsize=8,color='0.5')
ax3_1.set_ylabel('成绩',color='#BB9C48',fontsize=8)
ax3_1.set_title('成绩柱状图',fontsize=1,color='#BB9C48')
# ax3_1.grid(color='0.5',alpha=0.2)
# ax3_1.spines['left'].set_color('0.5')
# ax3_1.spines['right'].set_color('0.5')
# ax3_1.spines['top'].set_color('0.5')
# ax3_1.spines['bottom'].set_color('0.5')
#插入数据表
cell_colors=[['gray' for c in range(4)] for r in range(4)]
my_table=ax3_1.table(cellText=data_TB,loc='bottom',cellColours=cell_colors,
                     cellLoc='right',
                     colWidths=[0.15]*4
                     ,rowLabels=name
                     ,colLabels=course
                     ,rowColours=['red','gold','green','m']
                     ,colColours=['r','b','g','y']
                    ,fontsize=10,
                     bbox=[0.25,-0.7,0.5,0.5])
ax4=plt.axes([0.35,0.5,0.3,0.4])
rect=ax4.patch
rect.set_facecolor('#222A35')
#不显示x轴y轴的刻度线
plt.xticks(())
plt.yticks(())
data_English=data[data['课程名称']=='大学外语（英语）三']
x=data_English['姓名']
y=data_English['分数']
S_T=[0,0,0,0]
for i in y:
    if(i<60):
        S_T[0]=S_T[1]+1
    elif(60<=i<75):
        S_T[1]=S_T[1]+1
    elif (75<= i < 90):
        S_T[2] = S_T[2] + 1
    else:
        S_T[3] = S_T[3] + 1
    colors=['teal','dimgray','indigo','lightcoral']
print(S_T)
ax4_1=fig.add_axes([0.35,0.52,0.3,0.3])
ax4_1.pie(S_T,radius=1,explode=[0,0,0,0],autopct='%1.1f%%',colors={'pink','c','yellow','orange'},labels=['不及格','及格','良好','优秀'],
        textprops={'color':'b','fontsize':8})
ax4_1.set_title('《大学外语（英语）三》成绩等级百分比',fontproperties='SimHei',fontsize=12,color='pink',pad=10)
ax4_1.legend(bbox_to_anchor=(0.99,0.25),fontsize=6,loc='center left')#图例
ax5=plt.axes([0.67,0.5,0.3,0.4])
rect=ax5.patch
plt.xticks(())
plt.yticks(())
x=data_English['姓名']
y=data_English['分数']
ax5_1=fig.add_axes([0.7,0.55,0.25,0.3])
ax5_1.set_facecolor('white')
ax5_1.xaxis.set_major_locator(tk.MultipleLocator(5))
ax5_1.hist(y,bins=11,range=(45,100),edgecolor='pink',alpha=0.5,label='直方图',color='c')
ax5_1.tick_params('both',labelcolor='#BB9C48',labelsize=10,color='0.5')
ax5_1.set_ylabel('频数',color='black',fontsize=10)
ax5_1.set_xlabel('分数',color='black',fontsize=10)
ax5_1.set_title('《农业气象学》直方图',fontsize=12,color='c')
ax6=plt.axes([0,0.45,1,0.02])
rect=ax6.patch
rect.set_facecolor('#C38901')
plt.xticks(())
plt.yticks(())
ax7=plt.axes([0.03,0.05,0.3,0.4])
rect=ax7.patch
rect.set_facecolor('c')
plt.xticks(())
plt.yticks(())
plt.text(x=0.85,y=0.5,s="王\n明\n瑶\n成\n绩\n",fontproperties="SimHei",fontsize=12,color='pink',
         verticalalignment='center')
ax7_1=fig.add_axes([0.03,0.1,0.28,0.30],polar=True)
ax7_1.set_facecolor('pink')
data_cui=data[data['姓名']=='崔泽远']
score=list(data_cui['分数'])
label=data_cui['课程名称']
angle=np.linspace(0,2*np.pi,len(score),endpoint=False)
angles=np.concatenate((angle,[angle[0]]))
score=np.concatenate((score,[score[0]]))
ax7_1.set_thetagrids(angle*180/np.pi,label,color='#BB9C48',fontsize=4,fontproperties="Microsoft Yahei")
ax7_1.tick_params(labelsize=6,colors='purple')
ax7_1.plot(angles,score,"o-",lw=2)
ax7_1.set_theta_zero_location('NW')
ax7_1.set_rlim(0,100)
ax7_1.fill(angles,score,facecolor='g',alpha=0.2)
ax7_1.spines['polar'].set_color('0.7')
ax7_1.grid(color='0.6')
ax8=plt.axes([0.35,0.05,0.62,0.4])
rect=ax8.patch
rect.set_facecolor('purple')
plt.xticks(())
plt.yticks(())
ax8_1=fig.add_axes([0.38,0.1,0.58,0.31])
ax8_1.set_facecolor('c')
x=data_English['姓名']
y=data_English['分数']
xticks_stacked=['\n'.join(x) for x in x]#将x上刻度标签的汉字变成垂直排列
ax8_1.plot(xticks_stacked,y,':*',alpha=0.2,color=(0,0,1))
ax8_1.set_title('《毛泽东思想与中国特色社会主义理论体系概论》成绩分布',fontproperties='SimHei',fontsize=15,color='c',pad=5)
#通过figtext添加文字作为副标题
ax8_1.axhline(60,color='pink',ls='--')
ax8_1.axhline(90,color='pink',ls='--')
ax8_1.set_ylabel('成绩',color='#BB9C48',fontsize=10)
ax8_1.tick_params('y',labelcolor='#BB9C48',labelsize=10,color='0.5')
ax8_1.tick_params('x',labelcolor='#BB9C48',labelsize=8,color='0.5')
cursor = Cursor(ax8_1, useblit=True,color='yellow',linewidth=2)
plt.show()
